package com.cat.dataStructure02;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/reverse-substrings-between-each-pair-of-parentheses/
 * @create 2025/10/20 17:55
 * @since JDK17
 */

public class Solution30 {
    public String reverseParentheses(String s) {
        int n = s.length(), top = 0;
        char[] stack = new char[n];
        List<Character> list = new ArrayList<>();
        for (var c : s.toCharArray()) {
            if (c == ')') { // 弹出且反转
                list.clear();
                while (stack[top - 1] != '(') { // 一直弹
                    list.add(stack[--top]);
                }
                top--;
                for (var d : list) {
                    stack[top++] = d;
                }
            } else {
                stack[top++] = c;
            }
        }

        return new String(stack, 0, top);
    }
}
